class Solution
{
public:
    int countSubmatrices(vector<vector<int>> &grid, int k)
    {
        int m = grid.size();
        int n = grid[0].size();

        int result = 0;
        vector<vector<int>> prefixSum(m, vector<int>(n, 0));
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (i > 0)
                {
                    prefixSum[i][j] += prefixSum[i - 1][j];
                }
                if (j > 0)
                {
                    prefixSum[i][j] += prefixSum[i][j - 1];
                }
                if (i > 0 && j > 0)
                {
                    prefixSum[i][j] -= prefixSum[i - 1][j - 1];
                }
                prefixSum[i][j] += grid[i][j];
                if (prefixSum[i][j] <= k)
                {
                    result++;
                }
            }
        }
        return result;
    }
};